package com.demo.mqtt.common;

import com.demo.mqtt.common.utils.JacksonUtils;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jodd.bean.BeanCopy;

import java.io.Serializable;


/**
 * @program: mqtt
 * @description: 基础实体转换
 * @author: liuYunKai
 * @create: 2021-03-30 15:29
 */
public class BaseConvert implements Serializable {

    protected static final long serialVersionUID = 1L;

    /**
     * <p>
     * 获取自动转换后的JavaBean对象
     * </p>
     *
     * @param clazz 转换对象类
     * @param <T>   转换对象
     * @return 转换后的对象
     */
    public <T> T convert(Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            BeanCopy.beans(this, t).copy();
            return t;
        } catch (Exception e) {
            throw new RuntimeException("转换对象失败", e);
        }
    }

    /**
     * 转为 JSON 字符串
     *
     * @return 转换后的 JSON 格式的数据
     */
    public String toJson() {
        return JacksonUtils.toJSONString(this);
    }


}
